﻿namespace HDW_Migrate.Extensions;

/// <summary>
/// 字符串扩展方法
/// </summary>
public static class StringEx
{
    /// <summary>
    /// 字符串是否为空
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool IsEmpty(this string str) => string.IsNullOrWhiteSpace(str);

    /// <summary>
    /// 转化为日期字符串 yyyy-MM-dd
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string ToDate(this DateTime dateTime) => dateTime.ToString("yyyy-MM-dd");

    /// <summary>
    /// 将字节数组转化为Base64字符串
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string ToBase64(this byte[] bytes) => Convert.ToBase64String(bytes);

    /// <summary>
    /// 转化为日期字符串 yyyy-MM-dd
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime? ToDateTime(this string dateTime) => dateTime.IsEmpty() ? null : Convert.ToDateTime(dateTime);

    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static string ToDecryptContent(this string content)
    {
        var rsaCrypto = new RSACryptoService(RSACryptoService.privateKeyRSA, "");
        if (!content.IsEmpty() && content.Length > 100)
        {
            content = rsaCrypto.Decrypt(content);
        }
        return content;
    }

}
